package com.yzt.message.widget.chatinput.widget.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yzt.plugin_imageloader.ImageLoadUtil;
import com.yzt.message.R;
import com.yzt.message.widget.chatinput.widget.entity.MenuMoreAction;
import java.util.ArrayList;
import java.util.List;

/**
 * User: fish
 * E-mail: 709913163@qq.com
 * Date: 2021/12/9
 * Time: 13:43
 * describe:
 */
public class MenuMoreActionAdapter extends RecyclerView.Adapter<MenuMoreActionAdapter.ItemViewHolder>{

    private final Context activity;
    private List<MenuMoreAction> list;
    private MenuMoreActionAdapter.OnClickMenuMoreListener listener;

    public MenuMoreActionAdapter(Context activity) {
        this.activity = activity;
        this.list = new ArrayList<>();
    }

    public void setOnClickMenuMoreListener(MenuMoreActionAdapter.OnClickMenuMoreListener listener) {
        this.listener = listener;
    }

    public void initData(List<MenuMoreAction> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public MenuMoreActionAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new MenuMoreActionAdapter.ItemViewHolder(LayoutInflater.from(activity).inflate(R.layout.item_menu_more_action, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull MenuMoreActionAdapter.ItemViewHolder itemViewHolder, int position) {
        final MenuMoreAction menuMoreAction = list.get(position);
        itemViewHolder.tv_name.setText(menuMoreAction.getName());
        ImageLoadUtil.loadIv(activity, menuMoreAction.getIconUrl(), itemViewHolder.iv_icon);
        itemViewHolder.layout_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.selectIcon(menuMoreAction);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        private final ImageView iv_icon;
        private final TextView tv_name;
        private final LinearLayout layout_item;

        public ItemViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_name = itemView.findViewById(R.id.tv_name);
            layout_item = itemView.findViewById(R.id.layout_item);
        }
    }

    public interface OnClickMenuMoreListener {
        void selectIcon(MenuMoreAction menuMoreAction);
    }
}
